<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 500px;
            height: 500px;
            border: 1px solid black;
            margin: 50px auto;
            position: relative;
            text-align: center;
            font-size: 50px;
        }

        button {
            width: 100px;
            height: 50px;
            border: 1px solid red;
            position: absolute;
            bottom: 100px;
            left: 50%;
            margin-left: -50px;
        }
    </style>
</head>

<body>
    <div class="box">
        <p class="p">随机点菜</p>
        <button class="btn">开始点菜</button>
    </div>
    <script>
        // 获取元素
        var box = document.getElementsByClassName("box")[0];
        var btn = document.getElementsByClassName("btn")[0];
        var p = document.getElementsByClassName("p")[0];
        //给btn绑定点击事件
        var flag = true;
        btn.onclick = function () {
            if (flag) {
                btn.innerHTML = "结束"
                start(arr);
                flag = false

            } else {
                btn.innerHTML = "开始"
                stop();
                flag = true;
            }
            // console.log(flag)
        }
        var arr = ["青椒肉丝", "鱼香茄子", "土豆烧鸡", "肉末茄子", "鱼香肉丝", "干煸土豆丝", "红烧鱼块", "排骨藕汤", "辣子鸡", "可乐鸡翅"];
        var end;
        function start(arr) {    //开始
            end = setInterval(function () {
                for (index = 0; index < arr.length; index++) {
                    var rad = parseInt(Math.random() *arr.length )
                    // console.log(rad)
                    p.innerHTML = arr[rad];

                }
            }, 50)
        }


        function stop() {
            clearInterval(end);
        }

    </script>
</body>

</html>